<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线聊天窗口</title>
    <link rel="icon" href="/static/img/favicon.ico" type="image/x-icon"/>
    <link rel="shortcut icon" href="/static/img/favicon.ico" type="image/x-icon"/>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }

        #chat-container {
            max-width: 600px;
            width: 100%;
        }

        #chat-box {
            width: 100%;
            height: 400px;
            border: 1px solid #ccc;
            border-radius: 5px;
            overflow-y: scroll;
            padding: 10px;
            background-color: white;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        #chat-box p {
            margin: 5px 0;
        }

        #chat-box p strong {
            color: #007bff;
        }

        #chat-box p:nth-child(even) strong {
            color: #28a745;
        }

        #user-input {
            width: calc(100% - 70px);
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-top: 10px;
        }

        button {
            width: 60px;
            height: 40px;
            margin-top: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        @media (max-width: 600px) {
            #chat-box {
                height: 300px;
            }

            #user-input {
                width: calc(100% - 75px);
            }

            button {
                width: 65px;
            }
        }
    </style>
</head>
<body>
    <div id="chat-container">
        <div id="chat-box"></div>
        <input type="text" id="user-input" placeholder="输入消息...">
        <button onclick="sendMessage()">发送</button>
        <div id="loading" style="display:none; text-align:center; margin-top:10px;">正在发送...</div>
    </div>

    <script>
        function sendMessage() {
            const userInput = document.getElementById('user-input').value;
            if (!userInput.trim()) {
                alert('请输入消息后再发送！');
                return;
            }

            document.getElementById('loading').style.display = 'block';
            fetch('/send_message', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `message=${encodeURIComponent(userInput)}`
            })
            .then(response => response.json())
            .then(data => {
                const chatBox = document.getElementById('chat-box');
                chatBox.innerHTML += `<p><strong>你:</strong> ${userInput}</p>`;
                chatBox.innerHTML += `<p><strong>Bot:</strong> ${data.test}</p>`;
                chatBox.scrollTop = chatBox.scrollHeight;
                document.getElementById('user-input').value = '';
            })
            .finally(() => {
                document.getElementById('loading').style.display = 'none';
            });
        }
    </script>
</body>
</html>
